Ext.define('cloud.controller.Order', {
    extend: 'Ext.app.Controller',
    config: {
        refs: {
            order: 'order',
            unorder: 'unorder',
            passorder: 'passorder',
            finishorder: 'finishorder',
            shiporder: 'shiporder',
            search_order: 'order #search_order',
            orderstore: 'order #orderstore',
            search_unorder: 'unorder #search_unorder',
            search_passorder: 'passorder #search_passorder',
            search_finishorder: 'finishorder #search_finishorder',
            search_shiporder: 'shiporder #search_shiporder',
            sourceOverlay: {
                selector: 'sourceoverlay',
                xtype: 'sourceoverlay',
                autoCreate: true
            },
            print:'order #print',
            finish_print:'finishorder #finish_print',
            unorder_print:'unorder #unorder_print',
            ship_print:'shiporder #ship_print',
            pass_print:'passorder #pass_print'
        },
        control: {
            //点击显示详情
            'order dataview': {
                itemtap: function (view, index, target, record, event) {
                    this.detail(view, index, target, record, event);
                }
            },
            'unorder dataview': {
                itemtap: function (view, index, target, record, event) {
                    this.detail(view, index, target, record, event);
                }
            },
            'passorder dataview': {
                itemtap: function (view, index, target, record, event) {
                    this.detail(view, index, target, record, event);
                }
            },
            'finishorder dataview': {
                itemtap: function (view, index, target, record, event) {
                    this.detail(view, index, target, record, event);
                }
            },
            'shiporder dataview': {
                itemtap: function (view, index, target, record, event) {
                    this.detail(view, index, target, record, event);
                }
            },
            //搜索
            'search_order': {
                change: function () {
                    var store = Ext.getStore('Order');
                    var value = this.getSearch_order().getValue();
                    store.clearFilter();
                    if (value != "") {
                        store.removeAll(true);
                        var stores = Ext.getStore('SeachOrder').load({params: {keyword: value,'cadid':localStorage.getItem('cadid')}})
                        Ext.getCmp('orderstoreFind').setStore(stores);
                        Ext.getCmp('orderstoreFind').setHidden(false);
                        Ext.getCmp('orderstore').setHidden(true);
                        Ext.getCmp('titlebar').setHidden(false);
                    } else {
                        Ext.getCmp('orderstore').setHidden(false);
                        Ext.getCmp('orderstoreFind').setHidden(true);
                        Ext.getCmp('titlebar').setHidden(true);
                    }
                }
            },
            'search_unorder': {
                change: function () {
                    var store = Ext.getStore('UnOrder');
                    var value = this.getSearch_unorder().getValue();
                    store.clearFilter();
                    if (value != "") {
                        store.removeAll(true);
                        var param = '未处理';
                        var stores = Ext.getStore('SeachOrder').load({params: {keyword: value, param: param,'cadid':localStorage.getItem('cadid')}})
                        Ext.getCmp('UnOrderSeachID').setStore(stores);
                        Ext.getCmp('UnOrderSeachID').setHidden(false);
                        Ext.getCmp('UnOrderID').setHidden(true);
                        Ext.getCmp('UnOrderTitlebar').setHidden(false);
                        document.getElementById('allcheck').checked=false;
                    } else {
                        Ext.getCmp('UnOrderID').setHidden(false);
                        Ext.getCmp('UnOrderSeachID').setHidden(true);
                        Ext.getCmp('UnOrderTitlebar').setHidden(true);
                        document.getElementById('allcheck').checked=false;
                    }
                }
            }, 'search_passorder': {
                change: function () {
                    var store = Ext.getStore('PassOrder');
                    var value = this.getSearch_passorder().getValue();
                    store.clearFilter();
                    if (value != "") {
                        store.removeAll(true);
                        var param = '已处理';
                        var stores = Ext.getStore('SeachOrder').load({params: {keyword: value, param: param,'cadid':localStorage.getItem('cadid')}})
                        Ext.getCmp('PassOrderSeachID').setStore(stores);
                        Ext.getCmp('PassOrderSeachID').setHidden(false);
                        Ext.getCmp('PassOrderID').setHidden(true);
                        Ext.getCmp('PassOrderTitlebar').setHidden(false);
                        document.getElementById('allcheck').checked=false;
                    } else {
                        Ext.getCmp('PassOrderID').setHidden(false);
                        Ext.getCmp('PassOrderSeachID').setHidden(true);
                        Ext.getCmp('PassOrderTitlebar').setHidden(true);
                        document.getElementById('allcheck').checked=false;
                    }
                }
            }, 'search_finishorder': {
                change: function () {
                    var store = Ext.getStore('FinishOrder');
                    var value = this.getSearch_finishorder().getValue();
                    store.clearFilter();
                    if (value != "") {
                        store.removeAll(true);
                        var param = '已完成';
                        var stores = Ext.getStore('SeachOrder').load({params: {keyword: value, param: param,'cadid':localStorage.getItem('cadid')}})
                        Ext.getCmp('FinishOrderSeachID').setStore(stores);
                        Ext.getCmp('FinishOrderSeachID').setHidden(false);
                        Ext.getCmp('FinishOrderID').setHidden(true);
                        Ext.getCmp('FinishOrderTitlebar').setHidden(false);
                        document.getElementById('allcheck').checked=false;
                    } else {
                        Ext.getCmp('FinishOrderID').setHidden(false);
                        Ext.getCmp('FinishOrderSeachID').setHidden(true);
                        Ext.getCmp('FinishOrderTitlebar').setHidden(true);
                        document.getElementById('allcheck').checked=false;
                    }
                }
            }, 'search_shiporder': {
                change: function () {
                    var store = Ext.getStore('ShipOrder');
                    var value = this.getSearch_shiporder().getValue();
                    store.clearFilter();
                    if (value != "") {
                        store.removeAll(true);
                        var param = '已发货';
                        var stores = Ext.getStore('SeachOrder').load({params: {keyword: value, param: param,'cadid':localStorage.getItem('cadid')}})
                        Ext.getCmp('ShipOrderSeachID').setStore(stores);
                        Ext.getCmp('ShipOrderSeachID').setHidden(false);
                        Ext.getCmp('ShipOrderID').setHidden(true);
                        Ext.getCmp('ShipOrderTitlebar').setHidden(false);
                        document.getElementById('allcheck').checked=false;
                    } else {
                        Ext.getCmp('ShipOrderID').setHidden(false);
                        Ext.getCmp('ShipOrderSeachID').setHidden(true);
                        Ext.getCmp('ShipOrderTitlebar').setHidden(true);
                        document.getElementById('allcheck').checked=false;
                    }
                }
            },
            'print':{
                tap:function(){
                    var input = document.getElementsByName('record');
                    var store=Ext.getStore('Order');
                    console.log(store)
                    this.print(store,input);
                }
            },
            'finish_print':{
                tap:function(){
                    var store,input;
                    if(!Ext.getCmp('FinishOrderID').getHidden()){
                        store=Ext.getStore('FinishOrder');
                        input = document.getElementsByName('record');
                    }else{
                        store=Ext.getCmp('FinishOrderSeachID').getStore();
                        input = document.getElementsByName('search_record');
                    }
                    this.print(store,input);
                }
            },
            'unorder_print':{
                tap:function(){
                    if(!Ext.getCmp('UnOrderID').getHidden()){
                        var store=Ext.getStore('UnOrder');
                        var input = document.getElementsByName('record');
                    }else{
                        var store=Ext.getCmp('UnOrderSeachID').getStore();
                        var input = document.getElementsByName('search_record');
                    }
                    this.print(store,input);
                }
            },
            'ship_print':{
                tap:function(){
                    if(!Ext.getCmp('ShipOrderID').getHidden()){
                        var store=Ext.getStore('ShipOrder');
                        var input = document.getElementsByName('record');
                    }else{
                        var store=Ext.getCmp('ShipOrderSeachID').getStore();
                        var input = document.getElementsByName('search_record');
                    }
                    this.print(store,input);
                }
            },
            'pass_print':{
                tap:function(){
                    if(!Ext.getCmp('PassOrderID').getHidden()){
                        var store=Ext.getStore('PassOrder');
                        var input = document.getElementsByName('record');
                    }else{
                        var store=Ext.getCmp('PassOrderSeachID').getStore();
                        var input = document.getElementsByName('search_record');
                    }
                    this.print(store,input);
                }
            }
        }
    },

    print:function(store,input){
        var html='';
        for(var i=0;i<input.length;i++)
        {
            if(input[i].checked){
                var index=store.find('coid',input[i].value)
                var record = store.getAt(index)
                //产品收货信息
                html+='<div style="border-top:1px solid #000; padding: 20px; width: 100%">' +
                    '<table class="print_infor" style="width: 100%;line-height: 20px;">' +
                    '<tr>' +
                    '<td>订单号：'+record.get('ordersNo')+'</td>' +
                    '<td>收货人：'+record.get('consignee')+'</td>' +
                    '</tr>' +
                    '<tr>' +
                    '<td>电话：'+record.get('ctel')+'</td>' +
                    '<td>收货时间：'+record.get('takedate')+record.get('taketime').substr(0,4)+'</td>' +
                    '</tr>' +
                    '<tr><td colspan="2">收货地址：'+record.get('takeaddress')+'</td></tr>' +
                    '<tr><td colspan="2">留言信息：'+record.get('messages')+'</td></tr>' +
                    '</table>' ;
                var products = record.getData().products;
                //循环订单产品列表
                html+='<table class="print_product" style="width: 100%; margin-top: 6px; line-height: 20px;">' +
                    '   <tr>' +
                    '       <td>产品名称</td>' +
                    '       <td>规格</td>' +
                    '       <td>数量</td>' +
                    '       <td>价格</td>' +
                    '   </tr>' ;
                for(var j=0;j<products.length;j++){
                    html+=
                        '   <tr>' +
                            '       <td>'+products[j].pname+'</td>' +
                            '       <td>'+products[j].sname+'</td>' +
                            '       <td>'+products[j].pnumber+'</td>' +
                            '       <td>'+products[j].price+'</td>' +
                            '   </tr>'
                }

                html+='</table>' +
                    '<div style="line-height: 26px;">总价：'+record.get('totalmoney')+'元</div>'+
                    '<div style="line-height: 30px;">配送地址：____________________________________________________________________</div>'+
                    '</div>';
            }
        }
        html+='<div style="text-align: center; display: block; border-top:1px solid #000" id="print_button">' +
            '<input type="button" value="打印" onclick="document.getElementById(\'print_button\').style.display=\'none\';window.print();">' +
            '<input type="button" value="取消" onclick="window.close()">' +
            '</div>'
        var OpenWindow=window.open("", "fullscreen=1");
        OpenWindow.document.write("<TITLE>订单列表</TITLE>")
        OpenWindow.document.write("<BODY BGCOLOR=#ffffff>")
        OpenWindow.document.write(html)
        OpenWindow.document.write("</BODY>")
        OpenWindow.document.write("</HTML>")
        OpenWindow.document.close()

    },

    detail: function (view, index, target, record, event) {
        if (event.target.name == "detail") {
            var overlay = this.getSourceOverlay();
            if (!overlay.getParent()) {
                Ext.Viewport.add(overlay);
            }
            overlay.show();
            overlay.setMasked({
                xtype: 'loadmask',
                message: 'Loading...'
            });
            var recordData=record.getData();
            var html ='<div class="overlayBg">'+
                '<div class="overlay">订单详情</div>'+
                '<p>订单编号：' + recordData.ordersNo + '</p>' +
                '<p>收货人姓名：' + recordData.consignee + '</p>' +
                '<p>电话：' + recordData.ctel + '</p>' +
                '<p>地址：' + recordData.takeaddress + '</p>' +
                '<p>收货时间：' + recordData.takedate + '</p>' +
                //'<p>电话确认：'+recordData.isbill+'</p>'+
                '<p>发票信息：' + recordData.isbill + '</p>' +
                '<p>留言信息：' + recordData.messages + '</p>' +
                '<p>订单状态：' + recordData.status + '</p>' +
                '<hr>';
            var products = recordData.products;
            for (var i = 0; i < products.length; i++) {
                html += '<div style=" position:relative;"><p>产品名称：' + products[i].pname + '</p>' +
                    '<p>规格：' + products[i].sname + '</p>' +
                    '<p>单价：' + products[i].countprice + '</p>' +
                    '<p>数量：' + products[i].pnumber + '</p>' +
                    '<p>总价格：' + products[i].price + '</p>' +
                    '<div class="overlayImg"><img width="120px;" height="120px;" style="border-radius: 4px;" src="' + products[i].picurl + '" /></div>'+
                    '</div><hr>';
            }
            html+='</div>'
            overlay.setHtml(html);
            setTimeout(function () {
                overlay.unmask();
            }, 300);
        }
        if (event.target.name == "passorder") {
            this.updateOrder(record.getData().coid, '已处理','UnOrder')
        }
        if (event.target.name == "finish") {
            this.updateOrder(record.getData().coid, '已发货','PassOrder')
        }
        if (event.target.name == "success") {
            this.updateOrder(record.getData().coid, '已完成','FinishOrder')
        }
    },
    updateOrder: function (coid, status,store) {
        Ext.Msg.confirm(
            "提示",
            "是否处理订单为"+status+"状态！", function(buttonId) {
            if (buttonId === 'yes') {
                Ext.Viewport.mask({
                    xtype: 'loadmask',
                    message: '处理中...'
                });
                Ext.Ajax.request({
                    url: GLOBAL.api_url + 'orders_update',
                    method: 'POST',
                    params: {
                        coid: coid,
                        status: status
                    },
                    scope: this,
                    success: function (response) {
                        Ext.Viewport.unmask();
                        var result = Ext.decode(response.responseText);
                        if (result.Variables.Result.code == 0) {
                            Ext.Msg.alert(result.Variables.Message.messagestr);
                            Ext.getStore(store).load();
                            return;
                        } else {
                            return false;
                        }
                    },
                    failure: function () {
                        Ext.Viewport.unmask();
                        Ext.Msg.alert('请求失败', '服务器维护中....');
                    }
                });
            }else{
                return;
            }
        });
    }
});